package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.admin.entity.AdChannel;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dtos.PageResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.common.util.BeanHelper;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.media.dtos.WmNewsResultDTO;
import com.heima.model.user.dtos.AuthDto;
import com.heima.wemedia.client.WemediaFeign;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 频道信息表 服务实现类
 * </p>
 *
 * @author HM
 * @since 2022-04-26
 */
@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public PageResult<ChannelDto> findByPage(ChannelDto dto) {

//        封装分页
        Page<AdChannel> ipage = new Page<>(dto.getPage(), dto.getSize());
//        封装条件
        LambdaQueryWrapper<AdChannel> queryWrapper = new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(dto.getName())){
//            queryWrapper.like("name",dto.getName());
//            queryWrapper.lambda().like(AdChannel::getName,dto.getName());
            queryWrapper.like(AdChannel::getName,dto.getName());
        }
//        分页查询
        IPage<AdChannel> channelIPage = page(ipage, queryWrapper);
        if(channelIPage==null || CollectionUtils.isEmpty(channelIPage.getRecords())){
//            throw new RuntimeException("频道数据不存在！");
//            throw new LeadException(400,"频道数据不存在！");
            throw new LeadException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
//        复制属性
        List<ChannelDto> channelDtos = BeanHelper.copyWithCollection(channelIPage.getRecords(), ChannelDto.class);
//        构造返回结果
        return new PageResult<>(dto.getPage(),dto.getSize(),channelIPage.getTotal(),channelDtos);
    }



    @Autowired
    private WemediaFeign feign;
    /**
     * 分页查询自媒体
     *
     * @param dto
     * @return
     */
    public PageResult<WmNewsResultDTO> findByAllPage(ChannelDto dto) {
        PageResult<WmNewsResultDTO> list = feign.list(dto);
        System.out.println("list = " + list);
        return list;
    }


    /**
     * 文章详情
     *
     * @param id
     * @return
     */
    public WmNewsResultDTO findById(Integer id) {
     return  feign.findById(id);
    }


    /**
     * 审核不通过
     *
     * @param dto
     * @return
     */
    public void updateStatusNo(AuthDto dto) {
        feign.updateStatusNo(dto);
    }

    /**
     * 审核通过
     *
     * @param dto
     * @return
     */
    public void updateStatusOk(AuthDto dto) {
        feign.updateStatusOk(dto);
    }
}
